package com.koron.order.linepatrol.satellite.service.impl;

import cn.hutool.core.util.StrUtil;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.linedefect.base.bean.po.DefectBean;
import com.koron.order.linedefect.base.service.DefectService;
import com.koron.order.linepatrol.satellite.bean.po.LinePatrolMonitorChange;
import com.koron.order.linepatrol.satellite.bean.po.LinePatrolSatellite;
import com.koron.order.linepatrol.satellite.service.LinePatrolMonitorChangeService;
import com.koron.order.linepatrol.satellite.service.LinePatrolReferer;
import com.koron.order.linepatrol.satellite.service.LinePatrolSatelliteService;
import com.koron.order.linepatrol.uav.bean.VO.LinePatrolUnv;
import com.koron.order.linepatrol.uav.bean.VO.LineRiskPatrolUnvDetail;
import com.koron.order.linepatrol.uav.service.LineRiskPatrolUnvDetailService;
import com.koron.order.linepatrol.uav.service.LineUnvDefectPatrolDetailService;
import com.koron.order.risk.base.bean.po.LineRisk;
import com.koron.order.risk.base.service.LineRiskService;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * 巡视工单关联缺陷工单或风险管控单
 * @author jzp
 * @since 2023/3/3
 */
@Service
public class LinePatrolRefererImpl implements LinePatrolReferer {

    @Autowired
    LinePatrolMonitorChangeService linePatrolMonitorChangeService;

    @Autowired
    LinePatrolSatelliteService linePatrolSatelliteService;

    @Autowired
    LineRiskPatrolUnvDetailService lineRiskPatrolUnvDetailService;

    @Autowired
    LineUnvDefectPatrolDetailService lineUnvDefectPatrolDetailService;
    @Autowired
    DefectService lineDefectService;

    @Autowired
    LineRiskService lineRiskService;

    @Override
    public boolean refDefect(SessionFactory factory, DefectBean defectBean) {
        if (notOfCheckSource(defectBean.getSource(), defectBean.getPatrolId(), defectBean.getPatrolType())) {
            return true;
        }
        String patrolType = defectBean.getPatrolType();
        switch (patrolType) {
            case "satellite":
                return defectRefToSatellite(factory, defectBean);
            case "satellite_monitor":
                return defectRefToSatelliteMonitor(factory, defectBean);
            case "uav_defect":
                return defectRefToUavDefect(factory, defectBean);
            case "uav_risk":
                return defectRefToUavRisk(factory, defectBean);
        }
        return false;
    }
    @Override
    public boolean refRisk(SessionFactory factory, LineRisk lineRisk) {
        if (notOfCheckSource(lineRisk.getRiskSource(), lineRisk.getPatrolId(), lineRisk.getPatrolType())) {
            return true;
        }
        String patrolType = lineRisk.getPatrolType();
        switch (patrolType) {
            case "satellite":
                return riskRefToSatellite(factory, lineRisk);
            case "satellite_monitor":
                return riskRefToSatelliteMonitor(factory, lineRisk);
            case "uav_risk":
                return riskRefToUavRisk(factory, lineRisk);
        }
        return false;
    }
    @Override
    public boolean defectRefToSatellite(SessionFactory factory, DefectBean defectBean) {
        LinePatrolSatellite linePatrolSatellite = new LinePatrolSatellite();
        linePatrolSatellite.setId(defectBean.getPatrolId());
        linePatrolSatellite.setReportType("2");
        linePatrolSatellite.setReportOrderId(defectBean.getId());
        linePatrolSatellite.setReportStatus("1");
        linePatrolSatellite.setSignTime(new Date());
        linePatrolSatellite.setArriveWay(null);
        linePatrolSatellite.setChecker(ThreadLocalContext.get().getAccount());
        linePatrolSatellite.setCheckerName(ThreadLocalContext.get().getName());
        return linePatrolSatelliteService.update(factory, linePatrolSatellite);
    }

    @Override
    public boolean defectRefToSatelliteMonitor(SessionFactory factory, DefectBean defectBean) {
        LinePatrolMonitorChange linePatrolMonitorChange = new LinePatrolMonitorChange();
        linePatrolMonitorChange.setId(defectBean.getPatrolId());
        linePatrolMonitorChange.setReportType("2");
        linePatrolMonitorChange.setReportOrderId(defectBean.getId());
        linePatrolMonitorChange.setReportStatus("1");
        linePatrolMonitorChange.setSignTime(new Date());
        linePatrolMonitorChange.setArriveWay(null);
        linePatrolMonitorChange.setChecker(ThreadLocalContext.get().getAccount());
        linePatrolMonitorChange.setCheckerName(ThreadLocalContext.get().getName());
        return linePatrolMonitorChangeService.update(factory, linePatrolMonitorChange);
    }

    @Override
    public boolean defectRefToUavDefect(SessionFactory factory, DefectBean defectBean) {
        LinePatrolUnv linePatrolUnv = new LinePatrolUnv();
        linePatrolUnv.setId(defectBean.getPatrolId());
        linePatrolUnv.setReportType("2");
        linePatrolUnv.setReportOrderId(defectBean.getId());
        linePatrolUnv.setDefectiveReportStatus("1");
        linePatrolUnv.setDefectiveStatus("0");
        return lineUnvDefectPatrolDetailService.update(factory,linePatrolUnv);
    }

    @Override
    public boolean defectRefToUavRisk(SessionFactory factory, DefectBean defectBean) {
        LineRiskPatrolUnvDetail lineRiskPatrolUnvDetail = new LineRiskPatrolUnvDetail();
        lineRiskPatrolUnvDetail.setId(defectBean.getPatrolId());
        lineRiskPatrolUnvDetail.setReportType("2");
        lineRiskPatrolUnvDetail.setReportOrderId(defectBean.getId());
        lineRiskPatrolUnvDetail.setReportStatus("1");
        lineRiskPatrolUnvDetail.setSignTime(new Date());
        lineRiskPatrolUnvDetail.setArriveWay(null);
        lineRiskPatrolUnvDetail.setChecker(ThreadLocalContext.get().getAccount());
        lineRiskPatrolUnvDetail.setCheckerName(ThreadLocalContext.get().getName());
        return lineRiskPatrolUnvDetailService.update(factory,lineRiskPatrolUnvDetail);
    }

    @Override
    public boolean riskRefToSatellite(SessionFactory factory, LineRisk lineRisk) {
        LinePatrolSatellite linePatrolSatellite = new LinePatrolSatellite();
        linePatrolSatellite.setId(lineRisk.getPatrolId());
        linePatrolSatellite.setReportType("1");
        linePatrolSatellite.setReportOrderId(lineRisk.getId());
        linePatrolSatellite.setReportStatus("1");
        linePatrolSatellite.setSignTime(new Date());
        linePatrolSatellite.setArriveWay(null);
        linePatrolSatellite.setChecker(ThreadLocalContext.get().getAccount());
        linePatrolSatellite.setCheckerName(ThreadLocalContext.get().getName());
        return linePatrolSatelliteService.update(factory, linePatrolSatellite);
    }

    @Override
    public boolean riskRefToSatelliteMonitor(SessionFactory factory, LineRisk lineRisk) {
        LinePatrolMonitorChange linePatrolMonitorChange = new LinePatrolMonitorChange();
        linePatrolMonitorChange.setId(lineRisk.getPatrolId());
        linePatrolMonitorChange.setReportType("1");
        linePatrolMonitorChange.setReportOrderId(lineRisk.getId());
        linePatrolMonitorChange.setReportStatus("1");
        linePatrolMonitorChange.setSignTime(new Date());
        linePatrolMonitorChange.setArriveWay(null);
        linePatrolMonitorChange.setChecker(ThreadLocalContext.get().getAccount());
        linePatrolMonitorChange.setCheckerName(ThreadLocalContext.get().getName());
        return linePatrolMonitorChangeService.update(factory, linePatrolMonitorChange);
    }

    @Override
    public boolean riskRefToUavRisk(SessionFactory factory, LineRisk lineRisk) {
        LineRiskPatrolUnvDetail lineRiskPatrolUnvDetail = new LineRiskPatrolUnvDetail();
        lineRiskPatrolUnvDetail.setId(lineRisk.getPatrolId());
        lineRiskPatrolUnvDetail.setReportType("1");
        lineRiskPatrolUnvDetail.setReportOrderId(lineRisk.getId());
        lineRiskPatrolUnvDetail.setReportStatus("1");
        lineRiskPatrolUnvDetail.setSignTime(new Date());
        lineRiskPatrolUnvDetail.setArriveWay(null);
        lineRiskPatrolUnvDetail.setChecker(ThreadLocalContext.get().getAccount());
        lineRiskPatrolUnvDetail.setCheckerName(ThreadLocalContext.get().getName());
        return lineRiskPatrolUnvDetailService.update(factory,lineRiskPatrolUnvDetail);
    }

    /**
     * 判断是否不属于卫星巡视、无人机巡视
     */
    private boolean notOfCheckSource(String source, String patrolId, String patrolType) {
        if (source == null || !StrUtil.equalsAny(source, "satellite", "UAV")) {
            return true;
        }
        Assert.notNull(patrolId, "巡视工单Id不能为空，如果没有，清选择人工巡视");
        Assert.notNull(patrolType, "巡视工单类型不能为空，如果没有，清选择人工巡视");
        return false;
    }

}
